#include<stdio.h>
#include<graphics.h>
#include<conio.h>

#pragma comment(lib,"Winmm.lib")
//游戏画面尺寸
#define high 700
#define width 580
                                                //路径在F
IMAGE img_bk;		//背景图片

IMAGE img_planeNormal1,img_planeNormal2;//飞机图片
float plane_x,plane_y;					//飞机坐标

IMAGE img_planeExplode_1,img_planeExplode_2;//飞机爆炸

IMAGE img_bullet1,img_bullet2;			//子弹图片
float bullet_x,bullet_y;					//子弹坐标

IMAGE img_enermyPlane1,img_enermyPlane2;//敌机图片
float enermy_x,enermy_y;				//敌机坐标
int isExpolde=0;						//飞机是否爆炸
int score=0;							//得分



void starup()//游戏初始化 
{
	mciSendString("open F:\\game_music.mp3 alias bkmusic", NULL, 0, NULL);//播放背景音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);	//循环播放
	initgraph(width,high);
	
	loadimage(&img_bk,"F:\\background.jpg");				//导入背景
	
	loadimage(&img_planeNormal1,"F:\\planeNormal_1.jpg");	//导入正常飞机
	loadimage(&img_planeNormal2,"F:\\planeNormal_2.jpg");
	
	loadimage(&img_planeExplode_1,"F:\\planeExplode_1.jpg");	//导入爆炸飞机
	loadimage(&img_planeExplode_2,"F:\\planeExplode_2.jpg");
	
	
	loadimage(&img_bullet1,"F:\\bullet1.jpg");	//导入子弹
	loadimage(&img_bullet2,"F:\\bullet2.jpg");
	
	loadimage(&img_enermyPlane1,"F:\\enemyPlane1.jpg");	//导入敌机
	loadimage(&img_enermyPlane2,"F:\\enemyPlane2.jpg");
	
	
	plane_x=width*0.5;
	plane_y=high*0.7;
	
	bullet_x=plane_x;
	bullet_y=-100;
	
	enermy_x=width*0.5;
	enermy_y=10;
	BeginBatchDraw();
}




void show()
{
	
	putimage(0,0,&img_bk); //显示背景
	if(isExpolde==0)
	{
		putimage(plane_x-50,plane_y-60,&img_planeNormal1,NOTSRCERASE);//显示正常飞机
		putimage(plane_x-50,plane_y-60,&img_planeNormal2,SRCINVERT);
		
		putimage(bullet_x-7,bullet_y,&img_bullet1,NOTSRCERASE);//显示子弹
		putimage(bullet_x-7,bullet_y,&img_bullet2,SRCINVERT);
		
		putimage(enermy_x,enermy_y,&img_enermyPlane1,NOTSRCERASE);//显示敌机
		putimage(enermy_x,enermy_y,&img_enermyPlane2,SRCINVERT);
	}
	else
	{
		putimage(plane_x-50,plane_y-60,&img_planeExplode_1,NOTSRCERASE);//显示正常飞机
		putimage(plane_x-50,plane_y-60,&img_planeExplode_2,SRCINVERT);
	}
	outtextxy(width*0.48,high*0.95,"得分：");
	char s[5];
	sprintf(s,"%d",score);
	outtextxy(width*0.55,high*0.95,s);
	FlushBatchDraw();
	Sleep(5);
}



void updataWithoutInput()//用户无关设置
{
	if(isExpolde==0)
	{
		if(bullet_y>-25)
			bullet_y=bullet_y-2;
		
		if(enermy_y<high-25)
			enermy_y=enermy_y+0.5;
		else
			enermy_y=10;
		
		if(abs(enermy_x-bullet_x)+abs(enermy_y-bullet_y)<80)	//子弹击中敌机
		{
			enermy_x=rand()%width;
			enermy_y=-40;
			bullet_y=-85;
			mciSendString("close gemusic", NULL, 0, NULL);//先把前面一次的音乐关闭
			mciSendString("open F:\\gotEnermy.mp3 alias gemusic", 0, NULL, NULL);//打开音乐
			mciSendString("play gemusic", NULL, 0, NULL);//仅播放一次
			score++;
			if(score>0 && score%5==0 && score%2!=0)
			{
				mciSendString("close 5music", NULL, 0, NULL);//先把前面一次的音乐关闭
				mciSendString("open F:\\5.mp3 alias 5music", 0, NULL, NULL);//打开音乐
				mciSendString("play 5music", NULL, 0, NULL);//仅播放一次
			}
			if(score%10==0)
			{
				mciSendString("close 10music", NULL, 0, NULL);//先把前面一次的音乐关闭
				mciSendString("open F:\\10.mp3 alias 10music", 0, NULL, NULL);//打开音乐
				mciSendString("play 10music", NULL, 0, NULL);//仅播放一次
			}
		}
		if(abs(enermy_x-plane_x)+abs(enermy_y-plane_y)<150)	//敌机击中我
		{
			isExpolde=1;
			mciSendString("close exmusic", NULL, 0, NULL);//先把前面一次的音乐关闭
			mciSendString("open F:\\explode.mp3 alias exmusic", 0, NULL, NULL);//打开音乐
			mciSendString("play exmusic", NULL, 0, NULL);//仅播放一次
		}
	}
}

void updataWithInput()//用户有关设置
{
	if(isExpolde==0)
	{
		MOUSEMSG m;			//记录鼠标消息
		while(MouseHit())
		{
			m=GetMouseMsg();
			
			if(m.uMsg==WM_MOUSEMOVE)		//鼠标移动时，飞机坐标等于鼠标位置
			{
				plane_x=m.x;
				plane_y=m.y;
			}
			else if(m.uMsg==WM_LBUTTONDOWN)	//按下鼠标左键时
			{
				bullet_x=plane_x;
				bullet_y=plane_y-85;
			}
		}
	}
	else
	{
		EndBatchDraw();
		
	}
	
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}


void main()
{
	starup();//数据初始化
	while(1)//游戏循环 
	{
		show();//游戏显示 
		updataWithoutInput();//用户无关设置 
		updataWithInput(); //用户有关设置 
	}
	gameover();
} 